package com.ljkj.cordial.chat.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.dao.notify.FriendTable;
import com.ljkj.cordial.chat.widget.PuzzleImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class NavContactAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //显示头部
    private static final int VIEW_TYPE_ITEM_TITLE = 0;
    //显示内容
    private static final int VIEW_TYPE_ITEM_CONTENT = 1;

    private List<FriendTable> list = new ArrayList<>();

    public void setList(List<FriendTable> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        this.list = list;
        notifyDataSetChanged();
    }

    public void clear() {
        if (this.list != null) {
            this.list.clear();
            notifyDataSetChanged();
        }

    }

    public List<FriendTable> getList() {
        return list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        if (viewType == VIEW_TYPE_ITEM_TITLE) {
            View rootView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_contact_title, null,false);
            return new ViewTitleHolder(rootView);
        }
        View rootView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_contact, null,false);
        return new ViewContactHolder(rootView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int i) {
        int viewType = getItemViewType(i);
        FriendTable item = list.get(i);
        if (viewType == VIEW_TYPE_ITEM_TITLE) {
            ViewTitleHolder viewTitleHolder = (ViewTitleHolder) holder;
            viewTitleHolder.tvTitle.setText("" + item.getFirstLetter());
        } else {
            ViewContactHolder viewContactHolder = (ViewContactHolder) holder;
            viewContactHolder.itemTitle.setText(TextUtils.isEmpty(item.getFriendAlias()) ? item.getFriendNickname() : item.getFriendAlias());
            if (!TextUtils.isEmpty(item.getFriendAvatarPath())) {
                viewContactHolder.itemImg.setFriendPath(item.getFriendAvatarPath(), item.getFriendAvatar());
            } else {
                viewContactHolder.itemImg.setFriendUrl(item.getFriendAvatar(), item.getFriendUserId());
            }
            viewContactHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (contactItemClickListener != null) {
                        contactItemClickListener.onItemClick(v, item, i);
                    }
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        FriendTable pb = list.get(position);
        if (pb.isShowTitle()) {
            return VIEW_TYPE_ITEM_TITLE;
        } else {
            return VIEW_TYPE_ITEM_CONTENT;
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewTitleHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_title)
        TextView tvTitle;

        ViewTitleHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            setIsRecyclable(false);
        }
    }

    static class ViewContactHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_img)
        PuzzleImageView itemImg;
        @BindView(R.id.item_title)
        TextView itemTitle;

        ViewContactHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            setIsRecyclable(false);
        }
    }

    private OnContactItemClickListener contactItemClickListener;

    public void setContactItemClickListener(OnContactItemClickListener contactItemClickListener) {
        this.contactItemClickListener = contactItemClickListener;
    }

    public interface OnContactItemClickListener {
        void onItemClick(View view, FriendTable item, int position);
    }
}
